package cn.edu.zzuli.nothinglink.mapper;

import cn.edu.zzuli.nothinglink.entity.Classes;
import cn.edu.zzuli.nothinglink.entity.Course;
import cn.edu.zzuli.nothinglink.entity.Students;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author suzyyy
 * @since 2021-04-08
 */
public interface CourseMapper extends BaseMapper<Course> {

    @Insert("<script>INSERT INTO course_class(course_id, class_id) VALUES " +
                "<foreach collection='classIds' item='item' index='index' separator=',' >" +
                    "(#{courseId}, #{item})" +
                "</foreach>" +
            "</script>")
    boolean bindCourseAndClass(@Param("courseId") Integer courseId,
                               @Param("classIds") Collection<Integer> classIds);

    @Insert("<script>INSERT INTO student_course(stu_id, course_id, course_name, stu_name) VALUES " +
                "<foreach collection='stus' item='item' index='index' separator=',' >" +
                    "(#{item.stuId}, #{courseId}, #{courseName}, #{item.stuName})" +
                "</foreach>" +
            "</script>")
    boolean bindCourseAndStu(@Param("courseId") Integer courseId,
                             @Param("courseName") String courseName,
                             @Param("stus") List<Students> stus);

    /**
     * 其实获取的是
     * course_class 中间表中的 classId
     * @param courseId
     * @return
     */
    @Select("SELECT * FROM course_class cc WHERE cc.course_id = #{courseId} AND deleted = 0")
    List<Classes> getTheClassesInCourse(Integer courseId);

    @Update("<script>UPDATE course_class SET deleted = 1 WHERE course_id = #{courseId} AND deleted = 0 AND class_id IN" +
                "<foreach collection='classIds' item='item' index='index' open='(' separator=',' close=')'  >" +
                    "#{item}" +
                "</foreach>" +
            "</script>")
    boolean delTheBindCourseAndStu(@Param("courseId") Integer courseId,
                                   @Param("classIds") Collection<Integer> classIds);

    @Update("UPDATE student_course SET course_name = #{courseName} " +
            "WHERE course_id = #{courseId}")
    boolean updateTheCourseNameInStuCourse(@Param("courseName")String courseName,
                                           @Param("courseId")Integer courseId);


    @Select("SELECT c.* FROM course_class cc JOIN course c \n" +
            "ON cc.course_id = c.course_id \n" +
            "WHERE cc.class_id = #{classId} AND c.deleted = 0 AND cc.deleted = 0  " +
            "AND c.create_time <= #{now} AND c.end_time >= #{now}")
    List<Course> getMyCoursesInTheClass(@Param("classId") Integer classId, @Param("now") LocalDateTime now);

    /**
     * 获取的是班级的全部信息
     * @param courseId
     * @return
     */
    @Select("SELECT c.* FROM course_class cc JOIN classes c \n" +
            "ON cc.class_id = c.class_id WHERE cc.deleted = 0 and c.deleted = 0 " +
            "and cc.course_id = #{courseId}")
    List<Classes> getClassInTheCourses(@Param("courseId") Integer courseId);

    @Select("SELECT c.course_id FROM course_class cc JOIN course c \n" +
            "ON cc.course_id = c.course_id \n" +
            "WHERE cc.class_id = #{classId} AND c.deleted = 0 AND cc.deleted = 0  " +
            "AND c.create_time <= #{now} AND c.end_time >= #{now}")
    List<Integer> getMyCoursesIdInTheClass(@Param("classId") Integer classId, @Param("now") LocalDateTime now);
}
